package com.desksoft.ecommerce.util.NoticeHelp;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.mail.BodyPart;
import javax.mail.internet.MimeUtility;

import com.desksoft.ecommerce.helper.mall.SiteinfoHelper;
import com.desksoft.ecommerce.service.interf.logistics.IRegistryService;
import com.desksoft.ecommerce.service.interf.mall.IEmailMessageService;
import com.desksoft.ecommerce.util.holder.ApplicationContextHolder;
import com.desksoft.ecommerce.util.result.ServiceResult;
import com.desksoft.ecommerce.vo.mall.EventModelVO;
import com.desksoft.ecommerce.vo.mall.NoticeEventDefineVO;
import com.desksoft.ecommerce.vo.mall.SendSmsVO;
import com.desksoft.utility.mail.MailService;
import com.desksoft.utility.mail.message.MailMessage;



/***************************************
 * 
 * <pre>
 * 版权所有：杭州迪科软件 保留所有权利
 * 创建日期: 2013-03-06
 * 创建作者：wgl
 * 文件名称：SendEmailHelp.java
 * 版本：
 * 功能：
 * 最后修改时间：2013-03-06
 * 修改记录：
 * </pre>
 *****************************************/
public class SendNoticeHelp {
	
	public static Object sendNotice(Map<String, Object> paramers){
		
		//短信

		SendSmsVO sendSmsVO = new SendSmsVO();
		String evevtid = (String)paramers.get("eventid");
		sendSmsVO.setEventid(evevtid);
		sendSmsVO.setTelephone((String)paramers.get("telephone"));
		
		Map<String, String> pars = (Map<String, String>)paramers.get("param");
		sendSmsVO.setParamMap(pars);
		if(evevtid != null && evevtid.endsWith("_Message")){
			Sms(sendSmsVO);
			return Boolean.TRUE;
		}
		
		NoticeEventDefineVO no = new NoticeEventDefineVO();
		IEmailMessageService emailMessageService = ApplicationContextHolder
		.get("emailMessageService");
		no.setEventid((String)paramers.get("eventid"));
		ServiceResult<NoticeEventDefineVO> result = emailMessageService.selectNoticeEventDefineVO(no);
		if(result.isSuccess()){
			if(result.getResult().getStatus().equals("0")){
				return "0";//没有开启
			}
		}
		
		
		
		EventModelVO  eo = new EventModelVO();
		eo.setDccode(SiteinfoHelper.getInstance().getdCCode());
		eo.setEventid((String)paramers.get("eventid"));
		eo.setNoticetype("2");
		ServiceResult<EventModelVO> result2 = emailMessageService.selectEventModel(eo);
		if(!result2.isSuccess()){
			return "-1";//失败
		}
		String text = result2.getResult().getModeltext();
		String title = result2.getResult().getModeltitle();
		String  emailtext = emailtext(text,paramers);
		String[] emailarry = ((String)paramers.get("emailurl")).split(",");
		try {
//			MailService mailService = (MailService) ApplicationContextHolder.getBean("mailService");
//			MailMessage mailMessage = new MailMessage("utf-8");
//			BodyPart contentPart = mailMessage.createHtmlPart(emailtext);
//			contentPart.setContent(emailtext, "text/html;charset=utf-8");
//		//	contentPart.setText(emailtext);
//			mailMessage.appendMailPart(contentPart);
//			mailMessage.setBody(emailtext);
//			mailMessage.setSubject(MimeUtility.encodeText( title,"gb2312","B"));
//			mailService.sendMessage(mailMessage, emailarry);
			EmailThread emailThread=new EmailThread();
			emailThread.setTitle(title);
			emailThread.setEmailarry(emailarry);
			emailThread.setEmailtext(emailtext);
			Thread emailt=new Thread(emailThread);
		    emailt.start();
		
		
		}catch (Exception e) {
			return "-1";
		}
		return "1";//成功
		
	}
	
	public static Boolean Sms(SendSmsVO snedSmsVO){
		
		Map<String, Object> params= new HashMap<String, Object>();
		
		String velicode = getCode();
		Object obj = snedSmsVO.getParamMap().get("PWDNo");
		if(obj != null){
			snedSmsVO.getParamMap().put("PWDNo", velicode);
		}
		
		IRegistryService registryService = ApplicationContextHolder.get("registryService");
		ServiceResult<String> address = registryService.getSmsAddress();
		address.isSuccess();
		String url = address.getResult();
		url = url.replace("?", "");
		ServiceResult<String> keyWord = registryService.getSmsKeyWord();
		keyWord.isSuccess();
		String key = keyWord.getResult();
		String content = null;
		if(snedSmsVO.getEventid() != null && !"".equals(snedSmsVO.getEventid())){
			ServiceResult<String> con = registryService.getContent2(snedSmsVO.getParamMap(), snedSmsVO.getEventid(), snedSmsVO.getTelephone());
			con.isSuccess();
			content = con.getResult();
		}else{
			content = "传入的EcentID错误";
		}
		
		String dccode = SiteinfoHelper.getInstance().getdCCode();
		String eventid = snedSmsVO.getEventid();
		String telephone = snedSmsVO.getTelephone();
		
		params.put("url", url);
		params.put("dccode", dccode);
		params.put("eventid", eventid);
		params.put("key", key);
		params.put("mobile", telephone == null ? "" : telephone);
		params.put("sendtime", "");
		params.put("content", content);
		params.put("creater", "website");
		SmsThread smsThread=new SmsThread();
	    smsThread.setParams(params);
	    Thread smst=new Thread(smsThread);
	    smst.start();
		return Boolean.TRUE;
	}
	
	private static String getCode(){
		Random rnd = new Random();
		Integer a = rnd.nextInt();
		if(a < 0){
			a = 0 - a;
		}
		a = a % 1000000;
		return a.toString();
	}
	
	private static String emailtext(String text,Map<String, Object> paramers){
		String eventid = (String)paramers.get("eventid");
		if(eventid.equals("DDXDCG_Email")){
			text = text.replace("{order_uname}", (String)paramers.get("{order_uname}"));
			text = text.replace("{order_billNo}", (String)paramers.get("{order_billNo}"));
			text = text.replace("{order_billNoMoney}", (String)paramers.get("{order_billNoMoney}"));
			text = text.replace("{order_billNoDate}", (String)paramers.get("{order_billNoDate}"));
			text = text.replace("{order_billNoStatus}", (String)paramers.get("{order_billNoStatus}"));
			text = text.replace("{order_consignee}", (String)paramers.get("{order_consignee}"));
			text = text.replace("{order_methodOfPayment}", (String)paramers.get("{order_methodOfPayment}"));
			text = text.replace("{order_billNoValidity}", (String)paramers.get("{order_billNoValidity}"));
			return text;
		}
		if(eventid.equals("DDZFCG_Email")){
			text = text.replace("{pay_uname}", (String)paramers.get("{pay_uname}"));
			text = text.replace("{pay_billNo}", (String)paramers.get("{pay_billNo}"));
			text = text.replace("{pay_billNoMoney}", (String)paramers.get("{pay_billNoMoney}"));
			text = text.replace("{pay_billNoDate}", (String)paramers.get("{pay_billNoDate}"));
			text = text.replace("{pay_billNoStatus}", (String)paramers.get("{{pay_billNoStatus}"));
			text = text.replace("{pay_methodOfPayment}", (String)paramers.get("{pay_methodOfPayment}"));
			text = text.replace("{pay_consignee}", (String)paramers.get("{pay_consignee}"));
			return text;
		}
		if(eventid.equals("SJRZYZ_Email")){
			text = text.replace("{supplier_uname}", (String)paramers.get("{supplier_uname}"));
			text = text.replace("{supplier_click}", (String)paramers.get("{supplier_click}"));
			text = text.replace("{supplier_http}", (String)paramers.get("{supplier_http}"));
			return text;
		}
		if(eventid.equals("ZCYZ_Email")){
			text = text.replace("{verification_uname}", (String)paramers.get("{verification_uname}"));
			text = text.replace("{verification_click}", (String)paramers.get("{verification_click}"));
			text = text.replace("{verification_http}", (String)paramers.get("{verification_http}"));
			text = text.replace("{verification_aging}", (String)paramers.get("{verification_aging}"));
			return text;
		}
		if(eventid.equals("ZHMM_Email")){
			String password_uname = paramers.get("{password_uname}") == null ? "" :(String)paramers.get("{password_uname}");
			String password_click = paramers.get("{password_click}") == null ? "" :(String)paramers.get("{password_click}");
			String password_http = paramers.get("{password_http}") == null ? "" :(String)paramers.get("{password_http}");
			text = text.replace("{password_uname}", password_uname );
			text = text.replace("{password_click}", password_click );
			text = text.replace("{password_http}", password_http );
			return text ;
		}
		if(eventid.equals("YDDDBPC_Email")){
			String reserveP_uname = paramers.get("{reserveP_uname}") == null ? "" :(String)paramers.get("{reserveP_uname}");
			String reserveP_billno = paramers.get("{reserveP_billno}") == null ? "" :(String)paramers.get("{reserveP_billno}");
			String reserveP_date = paramers.get("{reserveP_date}") == null ? "" :(String)paramers.get("{reserveP_date}");
			text = text.replace("{reserveP_uname}", reserveP_uname );
			text = text.replace("{reserveP_billno}", reserveP_billno );
			text = text.replace("{reserveP_date}", reserveP_date );
			return text ;
		}
		if(eventid.equals("YDDWDBQX_Email")){
			String reserveQ_uname = paramers.get("{reserveQ_uname}") == null ? "" :(String)paramers.get("{reserveQ_uname}");
			String reserveQ_billno = paramers.get("{reserveQ_billno}") == null ? "" :(String)paramers.get("{reserveQ_billno}");
			text = text.replace("{reserveQ_uname}", reserveQ_uname );
			text = text.replace("{reserveQ_billno}", reserveQ_billno );
			return text ;
		}
		return "";
	}
}
